﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CPPEI.ETL.DataServiceInterface;
using CPPEI.ETL.Infrastructure;

namespace CPPEI.ETL.Domain.Services
{
    public class RelationalDatabaseService
    {
        /// <summary>
        /// 获取数据源返回列的信息
        /// </summary>
        /// <param name="serviceId"></param>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public List<Column> GetColumn(Guid serviceId, string value, int type, Dictionary<string, object> args)
        {

            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.DtSer> serRep = new EntityRepository<Entities.DtSer>(dbContext);
                EntityRepository<Entities.DtSource> dsRep = new EntityRepository<Entities.DtSource>(dbContext);
                EntityRepository<Entities.DtSerType> stRep = new EntityRepository<Entities.DtSerType>(dbContext);

                Entities.DtSer eDtSer = serRep.Get(p => p.Id == serviceId); //获取数据服务
                Entities.DtSerType eSerType = stRep.Get(p => p.Id == eDtSer.TypeId);

                IDataServiceProxy proxy = DtSerProxyFactory.Create(eSerType.ProxyAssembly, eSerType.ProxyType);
                DataSourceInfo dataSourceInfo = new DataSourceInfo(eDtSer.Address, eDtSer.NeedAuth, eDtSer.AuthUser,
                    CEncoder.Decode(eDtSer.AuthPassword)); //密码解密
                dataSourceInfo.DataSource = value;
                dataSourceInfo.DataSourceValue = value;
                dataSourceInfo.DataSourceType = type;
                var data = proxy.GetData(dataSourceInfo, args);
                return data.GetSchema();
            }


        }
    }
}
